package com.hong.service_msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.hong.service_msm.service.MsmService;
import com.hong.service_msm.utils.ConstantPropertiesUtils;
import com.hong.fy_model.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);


//        HttpClientConfig clientConfig = HttpClientConfig.getDefault();
//        clientConfig.setHttpProxy("http://47.57.188.208:80");
//        clientConfig.setHttpsProxy("http://127.0.0.1:33210");
//        profile.setHttpClientConfig(clientConfig);

        IAcsClient client = new DefaultAcsClient(profile); //创建客户端对象
        CommonRequest request = new CommonRequest(); //创建请求对象
        //request.setProtocol(ProtocolType.HTTPS); //设置协议类型
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
//        request.setSysMethod(MethodType.POST); //设置请求方式
//        request.setSysDomain("dysmsapi.aliyuncs.com"); //设置域名
//        request.setSysVersion("2022-7-10"); //设置版本号
//        request.setSysAction("SendSms"); //设置操作名称

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "小宏的短信测试服务");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_245180150");
        //验证码  需要使用使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    //mq发送短信封装
    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            boolean isSend = this.send(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }

    private boolean send(String phone, Map<String,Object> param) {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile); //创建客户端对象
        CommonRequest request = new CommonRequest(); //创建请求对象
        //request.setProtocol(ProtocolType.HTTPS); //设置协议类型
        request.setSysMethod(MethodType.POST); //设置请求方式
        request.setSysDomain("dysmsapi.aliyuncs.com"); //设置域名
        request.setSysVersion("2022-7-10"); //设置版本号
        request.setSysAction("SendSms"); //设置操作名称



        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "小宏的短信服务");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_245180150");

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
